use strict;

if (scalar(@ARGV) != 1)
{
	print "Usage: $0 2.3.4.5\n";
    exit;
}
#Format: 2.10.7.0
my $newVersion = $ARGV[0];
OpenReplace('Picturethrill\Properties\AssemblyInfo.cs', $newVersion);
OpenReplace('PicturethrillUpdate\Properties\AssemblyInfo.cs', $newVersion);
OpenReplace('PicturethrillUpdateService\Properties\AssemblyInfo.cs', $newVersion);
OpenReplace('PicturethrillUpdateServiceTest\Properties\AssemblyInfo.cs', $newVersion);
OpenReplace2('PicturethrillSetup\PicturethrillSetup.nsi', $newVersion);

sub OpenReplace
{
    my ($filename, $newVersion) = @_;
    print $filename, "\n";
    my $assemblyTempFile = "$ENV{TEMP}\\AssemblyInfo.cs";
    open(WFILE, ">", $assemblyTempFile);
    open(FILE, $filename);
    while(<FILE>)
    {
        s/^\[assembly\: AssemblyVersion\(\".*\"\)/\[assembly: AssemblyVersion\(\"$newVersion\"\)/;
        s/^\[assembly\: AssemblyFileVersion\(\".*\"\)/\[assembly: AssemblyFileVersion\(\"$newVersion\"\)/;
        print WFILE $_; 
    }
    close(FILE);
    close(WFILE);

    print $assemblyTempFile, "\n";

    system("copy $assemblyTempFile $filename");
}

sub OpenReplace2
{
    my ($filename, $newVersion) = @_;
    print $filename, "\n";
    my $assemblyTempFile = "$ENV{TEMP}\\PicturethrillSetup.nsi";
    open(WFILE, ">", $assemblyTempFile);
    open(FILE, $filename);
    while(<FILE>)
    {
        #!define VERSION "2.2.26.0"
        s/^\!define VERSION \".*\"/\!define VERSION \"$newVersion\"/;
        print WFILE $_; 
    }
    close(FILE);
    close(WFILE);

    print $assemblyTempFile, "\n";

    system("copy $assemblyTempFile $filename");
}